package com.zs.studentmanager.controller;

import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.common.Result;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.entity.vo.StudentVo;
import com.zs.studentmanager.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 周小白
 * @date: 2024-01-21 20:47
 **/
@RestController
@RequestMapping("/student")
@Tag(name = "学生管理")
public class StudentController {

    private final StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }


    /**
     * 获取学生信息表列表
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @param searchValue 搜索关键词
     * @return 学生信息表列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取学生信息表列表")
    public Result getStudentList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 @RequestParam(value = "searchValue", required = false) String searchValue) {
        if (pageNum < 1 || pageSize < 1) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        // 查询
        PageVo<StudentVo> studentVoPageVo = studentService.getStudentList(pageNum, pageSize, searchValue);
        if (studentVoPageVo == null) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success(studentVoPageVo);
    }


    /**
     * 添加学生
     * @param studentVo 学生信息
     * @return 添加结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加学生")
    @Transactional(rollbackFor = Exception.class)
    public Result addStudent(@RequestBody StudentVo studentVo) {
        if (studentVo == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        // 添加
        boolean save = studentService.addStudent(studentVo);
        if (!save) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }


    /**
     * 获取学生详情
     * @param id 学生id
     * @return 学生详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取学生详情")
    public Result getStudentDetails(@PathVariable Long id) {
        if (id == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        // 查询
        StudentVo studentVo = studentService.getStudentDetails(id);
        if (studentVo == null) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success(studentVo);
    }


    /**
     * 修改学生
     * @param studentVo 学生信息
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改学生")
    @Transactional(rollbackFor = Exception.class)
    public Result updateStudent(@RequestBody StudentVo studentVo) {
        if (studentVo == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        // 修改
        boolean update = studentService.updateStudent(studentVo);
        if (!update) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }




    /**
     * 删除学生
     * @param id 学生id
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除学生")
    public Result deleteStudent(@PathVariable Long id) {
        if (id == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        // 删除
        boolean remove = studentService.removeById(id);
        if (!remove) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }





}
